package com.myblog.myblog.controller;

import com.myblog.myblog.service.AdminBlogService;
import com.myblog.myblog.service.TagService;
import com.myblog.myblog.service.TypeService;
import com.myblog.myblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class BlogController {

    @Autowired
    private AdminBlogService adminBlogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @RequestMapping("/")
    public String header(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         Model model){
        model.addAttribute("page",adminBlogService.listBlog(pageable));
        model.addAttribute("types",typeService.listTypeTop(5));
        model.addAttribute("tags",tagService.listTagTop(5));
        model.addAttribute("recommendBlogs",adminBlogService.listBlogTop(5));
        return "head/blog";
    }

    @PostMapping("/search")
    public String search(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         @RequestParam String query, Model model){
        model.addAttribute("page",adminBlogService.listBlog("%"+query+"%",pageable));
        model.addAttribute("query",query);
        return "head/search";
    }

    @GetMapping("/footer/newBlog")
    public String newblogs(Model model){
        model.addAttribute("newBlogs",adminBlogService.listBlogTop(3));
        return "head/blog :: newBlogList";
    }

}